using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;

namespace GPSCore
{
    /// <summary>
    /// Main Convertion Class for GPS data
    /// </summary>
    public class GPSConvertManager
    {
        /// <summary>
        /// Used to convert knots into miles per hour
        /// 
        /// knots = nautical mile per hour = 1.852 kilometre per hour 
        /// </summary>
        public static double MPHPerKnot = double.Parse("1.150779", NmeaCultureInfo);

        /// <summary>
        /// From Knot to Km/H     1Knot = 1.852J Km / H
        /// </summary>
        public const double KnotToKmH = (double)1.852;

        /// <summary>
        /// From Km/H to m/s 1 Km/H = 1000/3600  m/s
        /// </summary>
        public const double KmHToMSec = (double)((1000.0) / (3600.0));

        /// <summary>
        /// Earth Radius in Km 
        /// </summary>
        public const double EarthRadiusKms = 6376.45; //WGS-84 (1984) 	6,378,137 	6,356,752.3142

        /// <summary>
        /// Represents the EN-US culture, used for numers in NMEA sentences
        /// </summary>
        public static CultureInfo NmeaCultureInfo = new CultureInfo("en-US");
        


        /// <summary>
        /// Convert a Degree value to Radians
        /// </summary>
        /// <param name="degrees"></param>
        /// <returns></returns>
        public static double DegreesToRadians(double degrees)
        {
            return ( degrees * Math.PI ) / 180.0;
        }

        /// <summary>
        /// Convert a Radians to Derees
        /// </summary>
        /// <param name="radians"></param>
        /// <returns></returns>
        public static double RadiansToDegrees(double radians)
        {
            return radians * 180.0 / Math.PI;
        }

        /// <summary>
        /// Convert Radians to Nautical Miles
        /// </summary>
        /// <param name="radians"></param>
        /// <returns></returns>
        public static double RadiansToNauticalMiles(double radians)
        {
            // There are 60 nautical miles for each degree
            return radians * 60 * 180 / Math.PI;
        }

        /// <summary>
        /// Convert Radians to Meters
        /// </summary>
        /// <param name="radians"></param>
        /// <returns></returns>
        public static double RadiansToMeters(double radians)
        {
            // there are 1852 meters in a nautical mile
            return 1852 * RadiansToNauticalMiles(radians);
        }

        /// <summary>
        /// Round the distance expressed in meters
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static double RoundDistance(double number)
        {
           return Math.Round( number , 8 );
        }



        //Private Function toDecimal(ByVal Pos As String) As Double
        //'Pos="5601.0318"
        //Dim PosDb As Double = CType(Replace(Pos, ".", ","), Double) 'Replace . with , (Used in danish doubles)
        //Dim Deg As Double = Math.Floor(PosDb / 100)
        //Dim DecPos As Double = Math.Round(Deg + ((PosDb - (Deg * 100)) / 60), 5)
        //Return DecPos '=56.0172
        //End Function

        /// <summary>
        /// Convert from UTM Coordinate to 
        /// </summary>
        /// <param name="UTMPosition"></param>
        /// <returns></returns>
        public static double UTMToDecimalDegrees(string UTMPosition)
        {
            double dUTMPosition = double.Parse(UTMPosition);
            double deg = Math.Floor(dUTMPosition / 100.0);

            return Math.Round(deg + ((dUTMPosition - (deg * 100)) / 60), 6);
        }

    
    }
}
